//
//  AEPointsAcquireViewController.m
//  AESOLAR_IOS
//
//  Created by Bin Tang on 2024/10/15.
//  Copyright © 2024 Mr.Wang. All rights reserved.
//

#import "AEPointsAcquireViewController.h"
#import "AE_Solar-Swift.h"
#import "AEPointsAcquireViewModel.h"
#import "AEPointsAcquireNavView.h"
#import "AEPointsAcquireTopView.h"
#import "AEPointsAcquireMainView.h"
#import "AECommunityPublishViewController.h"

@interface AEPointsAcquireViewController ()

@property (nonatomic, strong) AEPointsAcquireViewModel *viewModel;

@property (nonatomic, strong) AEPointsAcquireNavView *navView;

@property (nonatomic, strong) AEPointsAcquireTopView *topView;

@property (nonatomic, strong) AEPointsAcquireMainView *mainView;

@end

@implementation AEPointsAcquireViewController

- (void)dealloc {
    debugMethod();
}

- (void)initWithVar {
    [super initWithVar];
    
}

- (void)initWithViewModel {
    _viewModel = [AEPointsAcquireViewModel new];
    [_viewModel setupData];
    
}

- (void)initWithNavBar {
    [super initWithNavBar];
    
}

- (void)initWithSubView {
    [super initWithSubView];
    
    self.view.backgroundColor = [UIColor colorWithHexString:@"#F6F8F8"];
    
    self.baseTableView.hidden = YES;
    
    [self initWithRootContent:NO];
    self.rootContent.top = 0;
    self.rootContent.height = kScreenH;
    self.rootContent.backgroundColor = [UIColor colorWithHexString:@"#F6F8F8"];
    
    [self initWithNavView];
    
//    [self initWithTopView];
    
//    [self initWithmainView];
    
//    if (self.rootContent.height < self.mainView.bottom) {
//        self.rootContent.contentSize = CGSizeMake(kScreenW, self.mainView.bottom + 20);
//    }
    
    // 获取签到情况
    [self requestWithSignDatas];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [self.navigationController setNavigationBarHidden:YES animated:YES];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    if (self.viewModel.selectedModel) {
        [self.navigationController setNavigationBarHidden:YES animated:YES];
    } else {
        [self.navigationController setNavigationBarHidden:NO animated:YES];
    }
//    self.viewModel.selectedModel = nil;
}

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    if (self.viewModel.selectedModel) {
        [self.navigationController setNavigationBarHidden:YES animated:YES];
    } else {
        [self.navigationController setNavigationBarHidden:NO animated:YES];
    }
    self.viewModel.selectedModel = nil;
}

- (UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleLightContent;
}


- (void)initWithNavView {
    if (!_navView) {
        CGRect frame = CGRectMake(0, 0, kScreenW, NAVBAR_HEIGHT_STATUS);
        _navView = [[AEPointsAcquireNavView alloc] initWithFrame:frame];
        [self.view addSubview:_navView];
        @weakify(self);
        _navView.touchBackBlock = ^{
            @strongify(self);
            [self clickedBack:nil];
        };
    }
}

- (void)initWithTopView {
    if (!_topView) {
        CGRect frame = CGRectMake(0, 0, kScreenW, kAEPointsAcquireTopViewHeight);
        if (IPHONEX_TOP_SPACE == 0) {
            frame.size.height = frame.size.height - 24;
        }
        _topView = [[AEPointsAcquireTopView alloc] initWithFrame:frame];
        [self.rootContent addSubview:_topView];
        @weakify(self);
        _topView.touchSignInBlock = ^{
            @strongify(self);
            [self actionWithSignIn];
        };
    }
    [self.topView updateWithViewData:self.viewModel];
}

- (void)initWithmainView {
    if (!_mainView) {
        CGRect frame = CGRectMake(15, self.topView.bottom + 12, kScreenW - 30, 50 + kAEPointsAcquireMainSingleViewHeight * self.viewModel.dataList.count);
        _mainView = [[AEPointsAcquireMainView alloc] initWithFrame:frame];
        [self.rootContent addSubview:_mainView];
        @weakify(self);
        _mainView.touchGoFinishBlock = ^(TYZCommonCellModel * _Nonnull model) {
            @strongify(self);
            [self actionWithGoFinish:model];
        };
    }
    [self.mainView updateWithViewData:self.viewModel];
}

/// 点击签到
- (void)actionWithSignIn {
//    @weakify(self);
    [self.viewModel requestWithSignIn:^(EDSRespondDataEntity * _Nonnull respond) {
//        @strongify(self);
        if (respond.code == EDS_NETWORK_SUCCESS) {
            [SVProgressHUD showSuccessWithStatus:@"今日签到成功!"];
            [self initWithTopView];
        } else {
            if ([objectNull(respond.msg) isEqualToString:@""]) {
                [SVProgressHUD showInfoWithStatus:@"签到失败，请稍后再试！"];
            } else {
                [SVProgressHUD showInfoWithStatus:respond.msg];
            }
        }
    }];
}

/// 去完成
/// - Parameter model: model description
- (void)actionWithGoFinish:(TYZCommonCellModel *)model {
    UIViewController *vc = nil;
    self.viewModel.selectedModel = nil;
    if (model.index == 1) { // 完善个人信息
        vc = [[ChangeUserInfoViewController alloc] init];
    } else if (model.index == 2) { // 发布内容/评论（日常内容）
        [AEAppDelegateTool sharedInstance].selectedTabBarIndex = 0;
        [self.navigationController popViewControllerAnimated:NO];
    } else if (model.index == 3) { // 浏览内容
        [AEAppDelegateTool sharedInstance].selectedTabBarIndex = 0;
        [self.navigationController popViewControllerAnimated:NO];
    } else if (model.index == 4) { // 社群发布内容
        self.viewModel.selectedModel = model;
        AECommunityPublishViewController *publishVC = [[AECommunityPublishViewController alloc] init];
        publishVC.newsGroupId = [AEAppDelegateTool sharedInstance].newsGroupId;
        [TYZJumpVCManager pushBaseVC:self pushVC:publishVC];
    }
    if (vc) {
        [TYZJumpVCManager pushBaseVC:self pushVC:vc];
    }
}

/// 获取签到内容
- (void)requestWithSignInfos {
    @weakify(self);
    [self.viewModel requestWithSignInfos:^(EDSRespondDataEntity * _Nonnull respond) {
        @strongify(self);
        if (respond.code == EDS_NETWORK_SUCCESS) {
            [self requestWithSignTasks]; // 获取任务
            [self initWithTopView];
        }
    }];
}

/// 获取任务
- (void)requestWithSignTasks {
    @weakify(self);
    [self.viewModel requestWithSignTasks:^(EDSRespondDataEntity * _Nonnull respond) {
        @strongify(self);
        if (respond.code == EDS_NETWORK_SUCCESS) {
            [self initWithmainView];
        }
    }];
}

/// 获取签到情况
- (void)requestWithSignDatas {
    @weakify(self);
    [self.viewModel requestWithSignDatas:^(EDSRespondDataEntity * _Nonnull respond) {
        @strongify(self);
        // 获取签到内容
        [self requestWithSignInfos];
    }];
}

@end
